package com.ruoyi.web.controller.deal;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.deal.domain.HyDealCategory;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.deal.bo.HyDealBrandQueryBo;
import com.ruoyi.deal.domain.HyDealBrand;
import com.ruoyi.deal.service.IHyDealBrandService;
import com.ruoyi.deal.service.IHyDealCategoryBrandService;
import com.ruoyi.deal.vo.HyDealBrandVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 品牌管理Controller
 *
 * @author lixin
 * @date 2021-05-13
 */
@Api(value = "品牌管理控制器" , tags = {"品牌管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/deal/brand")
public class HyDealBrandController extends BaseController {

	private final IHyDealBrandService brandService;

	private final IHyDealCategoryBrandService categoryBrandService;

	/**
	 * 分页查询品牌列表
	 */
	@ApiOperation("查询品牌列表")
	@PreAuthorize("@ss.hasPermi('deal:brand:list')")
	@GetMapping("/list")
	public TableDataInfo<HyDealBrandVo> list(@Validated HyDealBrandQueryBo bo) {
		return brandService.queryPageList(bo);
	}

	/**
	 * 加载分类对应品牌列表
	 */
	@GetMapping("/listByCategoryId/{categoryId}")
	public AjaxResult listByCategoryId(@PathVariable("categoryId") Long categoryId) {

		List<HyDealBrand> brands = categoryBrandService.listByCategoryId(categoryId);

		return AjaxResult.success(brands);
	}

	/**
	 * 获取品牌管理详细信息
	 */
	@ApiOperation("获取品牌管理详细信息")
	@PreAuthorize("@ss.hasPermi('deal:brand:query')")
	@GetMapping("/{brandId}")
	public AjaxResult<HyDealBrandVo> getInfo(@PathVariable("brandId") Integer brandId) {
		return AjaxResult.success(brandService.queryById(brandId));
	}

	/**
	 * 新增品牌管理
	 */
	@ApiOperation("新增品牌管理")
	@PreAuthorize("@ss.hasPermi('deal:brand:add')")
	@Log(title = "品牌增加" , businessType = BusinessType.INSERT)
	@PostMapping()
	public AjaxResult add(@RequestBody HyDealBrand brand) {

		if (UserConstants.NOT_UNIQUE.equals(brandService.checkBrandNameUnique(brand))) {
			return AjaxResult.error("新增品牌'" + brand.getBrandName() + "'失败，品牌名称已存在");
		}

		return toAjax(brandService.insertByAddBo(brand));
	}

	/**
	 * 修改品牌管理
	 */
	@ApiOperation("修改品牌管理")
	@PreAuthorize("@ss.hasPermi('deal:brand:edit')")
	@Log(title = "品牌修改" , businessType = BusinessType.UPDATE)
	@PutMapping()
	public AjaxResult edit(@RequestBody HyDealBrand brand) {
		if (UserConstants.NOT_UNIQUE.equals(brandService.checkBrandNameUnique(brand))) {
			return AjaxResult.error("修改品牌'" + brand.getBrandName() + "'失败，品牌名称已存在");
		}
		if (brandService.updateBrand(brand) > 0) {
			return AjaxResult.success();
		}
		return AjaxResult.error("修改品牌'" + brand.getBrandName() + "'失败，请联系管理员");
	}

	/**
	 * 删除品牌管理
	 */
	@ApiOperation("删除品牌管理")
	@PreAuthorize("@ss.hasPermi('deal:brand:remove')")
	@Log(title = "品牌管理" , businessType = BusinessType.DELETE)
	@DeleteMapping("/{brandIds}")
	public AjaxResult<Void> remove(@PathVariable Integer[] brandIds) {
		return toAjax(brandService.deleteWithValidByIds(Arrays.asList(brandIds), true) ? 1 : 0);
	}

	/**
	 * 导出品牌管理列表
	 */
	@ApiOperation("导出品牌管理列表")
	@PreAuthorize("@ss.hasPermi('deal:brand:export')")
	@Log(title = "品牌管理" , businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<HyDealBrandVo> export(HyDealBrandQueryBo bo) {
		List<HyDealBrandVo> list = brandService.queryList(bo);
		ExcelUtil<HyDealBrandVo> util = new ExcelUtil<HyDealBrandVo>(HyDealBrandVo.class);
		return util.exportExcel(list, "品牌管理");
	}
}
